<?php

namespace Medicina\BackendBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Medicina\BackendBundle\Form\ResolucionType;
use Symfony\Component\HttpFoundation\Response;

class ResolucionController extends Controller {

    public function editAction($id) {

        $em = $this->getDoctrine()->getEntityManager();

        $entity = $em->getRepository('MedicinaKernelBundle:Resolucion')->find($id);
        $editForm = $this->createForm(new ResolucionType(), $entity);


        return $this->render('BackendBundle:Resolucion:form.html.twig', array(
                    'entity' => $entity,
                    'form' => $editForm->createView()
                ));
    }

    public function updateAction($id) {
        return $this->editEntity('MedicinaKernelBundle:Resolucion', $id);
    }

    private function editEntity($entity, $id) {
        $em = $this->getDoctrine()->getEntityManager();
        $entity = $em->getRepository($entity)->find($id);
        $return = array();
        if (!$entity) {
            $this->createMsgEdit($return, false, $entity, array(array("key" => "#ResolucionType_numero", "msj" => "No se encontro la resolucion")));
        } else {
            $editForm = $this->createForm(new ResolucionType(), $entity);
            $request = $this->getRequest();
            $editForm->bindRequest($request);
            try {
                if ($entity->getOficina() === null) {
                    $user = $this->container->get('security.context')->getToken()->getUser();
                    $oficina = $user->getOficina();
                    $entity->setOficina($oficina);
                }

                $em->persist($entity);
                $em->flush();
                $this->createMsgEdit($return, true, $entity);
            } catch (\PDOException $e) {
                $this->createMsgEdit($return, false, $entity, array(array("key" => "#ResolucionType_numero", "msj" => "El #numero esta en uso")));
            }

//                print_r($editForm->getErrors());
//                echo "<pre> <h5>Numero</h5>"; si uso el isValid entra en este! asique uso el try catch
//                print_r($editForm->get('numero')->getErrors());
//                echo "</pre> <h5>finNumero</h5>";
//                print_r( $editForm->get('fecha')->getErrors());
//                print_r( $editForm->get('Concurso')->getErrors());
//                print_r( $editForm->get('Oficina')->getErrors());
//                $this->createMsgEdit($return, false, $entity, array($editForm->getErrors()));
//            }
        }
        return $this->createAjaxResponse($return);
    }

    private function createMsgEdit(&$return, $okStatus = false, $entity, $errors = array()) {
        if ($okStatus) {
            $return['status'] = true;
            $return['mensaje'] = "Editado con exito";
            $return['entity'] = $entity->getJsonForAjaxEdit();
        } else {
            $return['status'] = false;
            $return['mensaje'] = "Ocurrio un error al intentar editar";
            $return['entity'] = $entity->getJsonForAjaxEdit();
            $return['errors'] = $errors;
        }
        return $return;
    }

    private function createAjaxResponse($return) {
        return new Response(json_encode($return), 200, array('Content-Type' => 'application/json'));
    }

}

?>
